package nl.infodation.emr.pricingservice.ui;

import java.util.List;

import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.data.PublishedHistorySearchItem;
import nl.infodation.emr.pricingservice.entity.DomainRule;
import nl.infodation.emr.pricingservice.ui.component.ExplicitPagingContainer;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Table;

@SuppressWarnings("rawtypes")
public class DomainCheckList extends Table {

	private static final long serialVersionUID = 1L;

	final private ExplicitPagingContainer pagingContainer = new ExplicitPagingContainer();

	private DomainRule domainRule;

	public Object[] STANDARD_COL_NAME;

	public String[] STANDARD_COL_HEADER;

	private void initColumnData() {
		STANDARD_COL_NAME = new Object[] { "publishedDate", };

		STANDARD_COL_HEADER = new String[] { SessionData.getI18N().getMessage(
				Constants.PUBLISHDATE_HEADER) };
	}

	public DomainCheckList(DomainRule domainRule) {
		this.domainRule = domainRule;
		initColumnData();
		setSizeFull();
		initializePagingContainer();
		setContainerDataSource(pagingContainer);
		setPageSize(13);
		setColumnHeaders(STANDARD_COL_HEADER);
		setColumnCollapsingAllowed(true);
		setColumnReorderingAllowed(true);
		setColumnProperties();
		setSelectable(true);
		setImmediate(true);
		setNullSelectionAllowed(false);
		this.setColumnWidth(STANDARD_COL_NAME[0],345);
		this.setColumnWidth(STANDARD_COL_HEADER[0],345);
	}

	public void loadPage(int page) {
		PublishedHistorySearchItem itemSearch = new PublishedHistorySearchItem();
		itemSearch.setDomainRuleID(domainRule.getDomainRuleId());
		List<BeanItem> items;
		items = ControllerFactory.getPublishedHistoryController()
				.getPaginationByCriteria(itemSearch, page, getPageSize());

		pagingContainer.setDataSource(items);
	}

	public int getPageSize() {
		return pagingContainer.getPageSize();
	}
	
	public void setPageSize(int pageSize) {
		pagingContainer.setPageSize(pageSize);
	}
	
	private void setColumnProperties() {
		setColumnAlignment("publishedDate", Table.ALIGN_CENTER);
	}

	private void initializePagingContainer() {
		pagingContainer.addContainerProperty("publishedDate", String.class,
				null);
	}

	public void setDataSource(List<BeanItem> publishDomains) {
		pagingContainer.setDataSource(publishDomains);
	}

}
